package com.ruoyi.business.controller;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.HistoricActivity;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.IProcessService;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 审核列表Controller
 * 
 * @author wolfcode
 * @date 2021-08-27
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController
{
    private String prefix = "business/carPackageAudit";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    @Autowired
    private IProcessService processService;

    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping()
    public String carMaintenanceInfo()
    {
        return prefix + "/carPackageAudit";
    }

    /**
     * 查询套餐审核列表
     */
    @RequiresPermissions("business:carPackageAudit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        carPackageAudit.setCreateBy(ShiroUtils.getUserId().toString()); //权限控制，sql中加where条件
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出审核列表列表
     */
    @RequiresPermissions("business:carPackageAudit:export")
    @Log(title = "审核列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        return util.exportExcel(list, "审核列表数据");
    }

    /**
     * 新增审核列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审核列表
     */
    @RequiresPermissions("business:carPackageAudit:add")
    @Log(title = "审核列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改审核列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        mmap.put("carPackageAudit", carPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核列表
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @Log(title = "审核列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除审核列表
     */
    @RequiresPermissions("business:carPackageAudit:remove")
    @Log(title = "审核列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }


    @RequiresPermissions("business:carPackageAudit:cancelApply")
    @PostMapping( "/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id)
    {
        carPackageAuditService.cancelApply(id);

        return success("撤销申请成功");
    }


    @RequestMapping("/processImg")
    public void processImg(Long id, HttpServletResponse response)
            throws Exception {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        InputStream inputStream=null;
        //如果状态为审核中或者审核拒绝，说明还正在流程中
        if(carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS) ||
                carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_REJECT)
        ){
            //获取活动节点
            List<String> activeActivityIds = processService.getActiveActivityIds(carPackageAudit.getInstanceId());
            inputStream = processService.getProcessImage(carPackageAudit.getProcessDefinitionId(),activeActivityIds,Collections.EMPTY_LIST);
        }else{
            inputStream = processService.getProcessImage(carPackageAudit.getProcessDefinitionId(), Collections.EMPTY_LIST,Collections.EMPTY_LIST);
        }
        IOUtils.copy(inputStream,response.getOutputStream());
    }


    @GetMapping("/todoPage")
    public String todoPage()
    {
        return prefix + "/todoPage";
    }

    /**
     * 查询套餐审核列表
     */
    @PostMapping("/todoList")
    @ResponseBody
    public TableDataInfo todoList(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.findTodoList(carPackageAudit);
        return getDataTable(list);
    }

    @GetMapping("/showVerifyDialog")
    public String showVerifyDialog(String taskId,ModelMap mmap)
    {
        mmap.put("taskId",taskId);
        return prefix + "/taskVerify";
    }

    @RequestMapping("/complete")
    @ResponseBody
    public AjaxResult  complete(String taskId, Boolean auditStatus, String comment)  {
        carPackageAuditService.complete(taskId, auditStatus,comment);
        return  AjaxResult.success("完成审批");
    }

    @GetMapping("/openServiceItemsEditPage")
    public String openServiceItemsEditPage(Long carPackageAuditId, ModelMap mmap)
    {
        CarPackageAudit audit = carPackageAuditService.selectCarPackageAuditById(carPackageAuditId);
        ServiceItem serviceItem = JSON.parseObject(audit.getServiceItemInfo(),ServiceItem.class);
        mmap.put("serviceItem", serviceItem);
        mmap.put("carPackageAuditId", carPackageAuditId);
        return prefix + "/serviceItemEdit";
    }

    @PostMapping("/serviceItemUpdate")
    @ResponseBody
    public AjaxResult editSave(Long carPackageAuditId,ServiceItem serviceItem)
    {
        carPackageAuditService.updateServiceItem(carPackageAuditId,serviceItem);
        return success("保存成功");
    }

    @PostMapping( "/reApply")
    @ResponseBody
    public AjaxResult reApply(String taskId,Long carPackageAuditId) {
        carPackageAuditService.reApply(taskId,carPackageAuditId);
        return success("重新发起申请成功");
    }

    @GetMapping("/donePage")
    public String donePage()
    {
        return prefix + "/donePage";
    }


    @PostMapping("/doneList")
    @ResponseBody
    public TableDataInfo doneList(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.findDoneList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 加载审批历史弹窗
     */
    @GetMapping("/historyList")
    public String historyList(String instanceId, ModelMap mmap)
    {
        mmap.put("instanceId", instanceId);
        return prefix + "/historyList";
    }

    @PostMapping("/listHistory")
    @ResponseBody
    public TableDataInfo listHistory(String instanceId) {
        List<HistoricActivity> list = carPackageAuditService.selectHistoryList(instanceId);
        return getDataTable(list);
    }
}
